package com.wzqj.agent.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzqj.agent.common.Page;
import com.wzqj.agent.web.dao.AgentDao;
import com.wzqj.agent.web.dao.RoleDao;
import com.wzqj.agent.web.dao.UserDao;
import com.wzqj.agent.web.domain.Agent;
import com.wzqj.agent.web.domain.Role;
import com.wzqj.agent.web.domain.User;
import com.wzqj.agent.web.service.AgentService;
/**
 * 代理商业务实现层
 * @author gz
 *
 */
@Service("agentService")
public class AgentServiceImpl implements AgentService {

	@Autowired
	private AgentDao agentDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	/**
	 * 
	 */
	@Override
	public void insert(Agent agent) {
		//新建代理商
		agentDao.insert(agent);
	}

	@Override
	public int delete(Integer id) {
		 agentDao.delete(id);
		 return id;
	}

	@Override
	public int update(Agent agent) {
		agentDao.update(agent);
		return agent.getId();
	}

	@Override
	public Agent getById(Integer id) {
		return agentDao.getById(id);
	}

	@Override
	public List<Agent> getAll() {
		return agentDao.getAll();
	}

	@Override
	public List<Agent> getByUser(Integer userId) {
		//获取用户
		User user  = userDao.getById(userId);
		if (user==null) {
			return null;
		}
		Integer agentId = user.getTypeId();
		List<Agent> agentList = agentDao.getByParentId(agentId);
		//判断用户角色role
		//Integer roleId = user.getRoleId();
		
		//Role role = roleDao.getById(roleId);
		
		/*if (roleId!=null&&0==roleId) {//若用户为admin
			//List<User> users = userDao.getByRole(1);//查出所有的一级代理商用户
			//List<User> allUsers = userDao.getAll();
			for (User allUser : allUsers) {
				Agent agent = agentDao.getById(allUser.getTypeId());
				agents.add(agent);
			}
		}else if (roleId!=null&&1==roleId) {////若用户为一级代理商用户
			Integer agentId = user.getTypeId();
			List<Agent> agentList = agentDao.getByParentId(agentId);//查出所有的二级代理商用户
			if (agentList==null||agentList.size()<=0) {
				return null;
			}
			for (Agent agent : agentList) {
				Map<String,Object> typeParams= new HashMap<String, Object>();
				typeParams.put("type", 1);
				typeParams.put("typeId", agentId);
				agents.add(agent);
			}
		}else{
			agents = null;
		}*/
		
		return agentList;
	}
	@Override
	public Page<Agent> getByUserPage(Integer userId,Integer pageNo){	
		
		//获取用户
		User user = userDao.getById(userId);
		if (user == null) {
			return null;
		}
		Integer agentId = user.getTypeId();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("parentId", agentId);
		
		int start = (pageNo-1)*Page.defaultPageSize;
		param.put("start", start);
		param.put("end", Page.defaultPageSize);
		List<Agent> agentList = agentDao.getByParentIdPage(param);
		int total = agentDao.getTotalByParentId(agentId);
		return new Page<Agent>(total, pageNo, agentList);
	}
	
	@Override
	public Agent getByContactPhone(String contactPhone) {
		return agentDao.getByContactPhone(contactPhone);
	}

}
